import 'package:casdon/app/config/env/common_config.dart';
import 'package:casdon/core/utils/shared_preferences_utils.dart';

/// 统一管理首启判断的服务
class FirstLaunchService {
  FirstLaunchService._();

  static final FirstLaunchService _instance = FirstLaunchService._();

  static FirstLaunchService get instance => _instance;

  /// true 表示首次启动，false 表示已经启动过
  Future<bool> isFirstLaunch() async {
    final sp = await SPUtils.getInstance();
    final hasLaunched = sp.getBool(CommonConfig.hasLaunched, defValue: false);
    return !hasLaunched;
  }

  /// 标记应用已启动过（用于删除clear 钥匙串token信息）
  Future<void> markLaunched() async {
    final sp = await SPUtils.getInstance();
    await sp.setBool(CommonConfig.hasLaunched, true);
  }

  /// （首次启动时） 是否已弹出过弹窗 用户协议/隐私政策
  Future<bool> hasAcceptedAgreements() async {
    final sp = await SPUtils.getInstance();
    return sp.getBool(CommonConfig.hasAcceptedAgreements, defValue: false);
  }

  Future<void> markAgreementsAccepted() async {
    final sp = await SPUtils.getInstance();
    await sp.setBool(CommonConfig.hasAcceptedAgreements, true);
  }

  /// （首次启动时）进入首页是否需要展示引导蒙层
  Future<bool> shouldShowGuideOverlay() async {
    final sp = await SPUtils.getInstance();
    final shown = sp.getBool(
      CommonConfig.hasShownGuideOverlay,
      defValue: false,
    );
    return !shown;
  }

  Future<void> markGuideOverlayShown() async {
    final sp = await SPUtils.getInstance();
    await sp.setBool(CommonConfig.hasShownGuideOverlay, true);
  }
}
